<?php
/**
**2015-09-18 对接智游宝切换两次交互模式
**User By  PZP
**/
include('/var/www/html/new/d/module/api/ZhiYouBao/zhiyoubao.class.php');
include('/var/www/html/new/d/module/api/function.class.php');

$ac='16ucom';
$pw='c33367701511b4f6020ec61ded352059';
$inSide = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

$PFT_infunction = new AllFunction(); 
$PFT_infunction->logfile('7|'.json_encode($_REQUEST),'get_data');
// http://www.12301.cc/module/api/ZhiYouBao/zyb_get_data.php?Tnum=1&Fid=113&PayStatus=1&TicketID=24380&TouristName=pzp&TouristTel=15959110796&PlayDate=2015-09-22 00:00:00&IdCard=350321199108230123
// http://www.12301.cc/module/api/ZhiYouBao/zyb_get_data.php?Tnum=0&Action=MOD&Ordern=2341346

if(!isset($_REQUEST['Action'])){	
	if($_REQUEST['PayStatus'] == 2) exit("200");
	if(!$_REQUEST['TicketID']) exit("101");

	$tid = $_REQUEST['TicketID'] + 0;
	$select_uuid = "select t.uuid,t.pid,t.title as ttitle,t.landid,l.title as ltitle,l.apply_did,m.account from uu_jq_ticket as t, uu_land as l,pft_member as m where t.id=$tid  and t.landid=l.id and l.apply_did=m.id  limit 1";
	$GLOBALS['le']->query($select_uuid);
	if(!$GLOBALS['le']->fetch_assoc())	exit("101");		
	$uuid = $GLOBALS['le']->f('uuid');  //查询票类id	
	$pid = $GLOBALS['le']->f('pid');  
	$productname = $GLOBALS['le']->f('ttitle'); 
	$lname = $GLOBALS['le']->f('ltitle'); 
	$lid = $GLOBALS['le']->f('landid');  
	$apply_did = $GLOBALS['le']->f('apply_did'); 
	$account = $GLOBALS['le']->f('account'); 

	$zyb = new ZhiYouBao($apply_did);
	
	// $UUtprice = $PFT_infunction->Get_price($pid,substr($_REQUEST['PlayDate'],0,10),0) / 100 ;
	
	$get_ghprice = $PFT_infunction->Dynamic_Price_And_Storage($account,$pid,substr($_REQUEST['PlayDate'],0,10),0,$apply_did);
	$get_ghprice_xml = simplexml_load_string($get_ghprice);
	$UUtprice = (string)$get_ghprice_xml->Rec->UUsprice / 100;
	

	$ls_order = 'pft'.str_replace('.',mt_rand(1000,9999),microtime(true));	
	$request_array = array(
		'UUordertime'	=> date('Y-m-d'),
		'pftordernum' 	=> $ls_order,
		'uuid'			=> $uuid,	
		'pftordername' 	=> $_REQUEST['TouristName'],
		'UUordertel'	=> $_REQUEST['TouristTel'],
		'IDcar'			=> $_REQUEST['IdCard'],
		'UUtnum' 		=> $_REQUEST['Tnum'],
		'productname'	=> $productname,
		'UUplaytime'	=> substr($_REQUEST['PlayDate'],0,10),
		'UUtprice'		=> $UUtprice,
		'UUltitle'		=> $lname		
	);

	$request = $zyb->zyb_submit_order($request_array,$PFT_infunction);
	if($request == 101) exit("101|0|0|0|0|0");
	else{
		exit("200|0|0|0|".(string)$request->orderResponse->order->assistCheckNo."|".(string)$request->orderResponse->order->orderCode.'_'.$ls_order);
	}
	
}elseif($_REQUEST['Action'] == 'Relation_after_pay'){ //订单支付后通知
	$pft_order_xml = $PFT_infunction->Select_PFTorder($_REQUEST['Ordern']);
	$lid = (int)$pft_order_xml->Rec->UUlid;
	$tid = (int)$pft_order_xml->Rec->UUtid;				//门票id
	$ordername = (string)$pft_order_xml->Rec->UUordername;	//订单客人姓名
	$UUtnum = (int)$pft_order_xml->Rec->UUtnum;				//订单数量
	$UUplaytime=(string)$pft_order_xml->Rec->UUbegintime;	//游玩时间
	$UUtprice=(string)$pft_order_xml->Rec->UUtprice / 100;		//结算价
	$UUordertel=(string)$pft_order_xml->Rec->UUordertel;	//订单客人手机号码
	$UUstatus = (int)$pft_order_xml->Rec->UUstatus;			//订单状态
	$IDcar =(string)$pft_order_xml->Rec->UUpersonid;		//订单客人身份证号
	
	$select_uuid = "select t.uuid,t.pid,t.title as ttitle,t.landid,l.title as ltitle,l.apply_did from uu_jq_ticket as t, uu_land as l where t.id=$tid  and t.landid=l.id limit 1";
	$GLOBALS['le']->query($select_uuid);
	if(!$GLOBALS['le']->fetch_assoc())	exit("101");		
	$uuid = $GLOBALS['le']->f('uuid');  //查询票类id	 
	$productname = $GLOBALS['le']->f('ttitle'); 
	$lname = $GLOBALS['le']->f('ltitle'); 
	$apply_did = $GLOBALS['le']->f('apply_did');
	
	$zyb = new ZhiYouBao($apply_did);
	
	$request_array = array(
		'UUordertime'	=> date('Y-m-d'),
		'pftordernum' 	=> $_REQUEST['Ordern'],
		'uuid'			=> $uuid,	
		'pftordername' 	=> $ordername,
		'UUordertel'	=> $UUordertel,
		'IDcar'			=> $IDcar,
		'UUtnum' 		=> $UUtnum,
		'productname'	=> $productname,
		'UUplaytime'	=> $UUplaytime,
		'UUtprice'		=> $UUtprice,
		'UUltitle'		=> $lname		
	);
	// print_r($request_array);exit;
	$request = $zyb->zyb_submit_order($request_array,$PFT_infunction);
	if($request == 101) exit("101|0|0|0|0|0");
	else{
		$Ordern  =  $_REQUEST['Ordern'];
		$apiOrder = (string)$request->orderResponse->order->orderCode;
		$tempOrder = $_REQUEST['Ordern'];
		$apiCode = (string)$request->orderResponse->order->assistCheckNo;
		
		$str="insert into all_api_order (pftOrder,apiOrder,tempOrder,coopB,oStatus,oStnum,apiCode,handleStatus,cTime) values('$Ordern','$apiOrder','$tempOrder',7,0,$UUtnum,'$apiCode',0,now())";
		$GLOBALS['le']->query($str);
		
		exit("200|0|0|0|".$apiCode."|".$apiOrder.'_'.$Ordern);
	}
	
}elseif($_REQUEST['Action'] == 'Relation'){ //下单成功

	$Ordern = $_REQUEST['Ordern'];
	$pft_order_xml = $PFT_infunction->Select_PFTorder($_REQUEST['Ordern']);
	$tnum = (int)$pft_order_xml->Rec->UUtnum;
	$UUpaystatus = (int)$pft_order_xml->Rec->UUpaystatus;
	$apiCode = (string)$pft_order_xml->Rec->UUvcode;
	$tordernums = explode('_',(string)$pft_order_xml->Rec->UUtordernum);
	$apiOrder = $tordernums['0'];
	$tempOrder = $tordernums['1'];
	if($UUpaystatus == 1){
		$str="insert into all_api_order (pftOrder,apiOrder,tempOrder,coopB,oStatus,oStnum,apiCode,handleStatus,cTime) values('$Ordern','$apiOrder','$tempOrder',7,0,$tnum,'$apiCode',0,now())";
		$GLOBALS['le']->query($str);
	}
	
}elseif($_REQUEST['Action'] == 'MOD'){//取消订单
	if($_REQUEST['Ordern'] == '2819233' || $_REQUEST['Ordern'] == '2819229') exit("200");
	$pftOrder = $_REQUEST['Ordern'];
	$selapiOrder = "select apiOrder,tempOrder,handleStatus,oStatus,oStnum from all_api_order where pftOrder='$pftOrder' limit 1";
	$GLOBALS['le']->query($selapiOrder);
	if(!$GLOBALS['le']->fetch_assoc())	exit('105');
	$handleStatus = $GLOBALS['le']->f('handleStatus');
	$tempOrder = $GLOBALS['le']->f('tempOrder');
	$oStatus = $GLOBALS['le']->f('oStatus');
	$tnum = $GLOBALS['le']->f('oStnum');
	$apiOrder = $GLOBALS['le']->f('apiOrder');
		
	$pft_order_xml = $PFT_infunction->Select_PFTorder($pftOrder);
	$lid = (int)$pft_order_xml->Rec->UUlid;
	$UUaids= (string)$pft_order_xml->Rec->UUaids;
	if($UUaids == 0){
		$UUaid = (int)$pft_order_xml->Rec->UUaid;
	}else{
		$UUaidss= explode(',',$UUaids);
		$UUaid = $UUaidss[0];
	}
	
	$zyb = new ZhiYouBao($UUaid);	
	
	if($handleStatus == 0){
		if($_REQUEST['Tnum'] == 0){ //取消
			if($oStatus == 3) exit("200");
			$result= $zyb->zyb_cancel($tempOrder,$pftOrder,$apiOrder,$PFT_infunction);
			exit("1077");
		}
		else{//修改
			if($tnum == $_REQUEST['Tnum'] && $oStatus == 4) exit("200");
			$rnum = $tnum - $_REQUEST['Tnum'];
			$result= $zyb->zyb_modify($tempOrder,$rnum,$pftOrder,$apiOrder,$PFT_infunction);
			exit("1077");
		}
	}

}
?>